<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0131)https://blackboard.umkc.edu/@@94aef5bea58d9efcf3ec1cf509c03165/courses/1/umkc-cs-352-voa-vob-fs2005/content/_131136_1/randomWeiss.h -->
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2769" name=GENERATOR></HEAD>
<BODY><PRE>//Random number class written my Mark Weiss for Data Structures and Algorithm Analysis in //C++

//This class generates random numbers that do no overflow on 32-bit machines
// Filename:  random-Weiss.h

#ifndef randomWeiss_h
#define randomWeiss_h

static const int A = 48271;
static const int M = 2147483647;
static const int Q = M / A;
static const int R = M % A;

class Random{
public:
	explicit Random(int initialValue=1);

	int randomInt();
	double random0_1();
	int randomInt( int low, int high );

private:
	int state;
};

//Construct with initialValue for the state
Random::Random ( int initialValue )
{
	if( initialValue &lt; 0 )
		initialValue += M;

	state = initialValue;
	if (state == 0 )
		state = 1;
}

//Return a pseudorandom int and change the internal state
int Random::randomInt()
{
	int tmpState = A * ( state % Q ) - R * ( state / Q );
	if (tmpState &gt;= 0)
		state = tmpState;
	else
		state = tmpState + M;
	return state;
}


//Return a pseudorandom double in the open range 0..1 and
//change the internal state
double Random::random0_1()
{
	return (double) randomInt() / M;
}



//Return an int in the closed range [low,high], and
//change the internal state.

int Random::randomInt( int low, int high )
{
       double partitionSize = (double) M / ( high - low + 1 );

       return (int) ( randomInt( ) / partitionSize ) + low;
}


#endif</PRE></BODY></HTML>
